package com.systemdevelopment.mainproject.View.Admin;

import com.jfoenix.controls.JFXButton;
import com.jfoenix.controls.JFXTextField;
import com.systemdevelopment.mainproject.Model.Author;
import com.systemdevelopment.mainproject.Model.Book;
import com.systemdevelopment.mainproject.View.mainUI;
import com.systemdevelopment.mainproject.View.oneInstanceController;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.collections.ObservableSet;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.control.cell.TextFieldTableCell;
import javafx.util.Callback;

import javax.swing.text.html.ImageView;
import java.awt.*;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class BookTableController extends oneInstanceController {

    @FXML
    private JFXButton deleteBookButton;

    @FXML
    private JFXButton showAllBookButton;

    @FXML
    private TableColumn<Book, String> AllQuantity;

    @FXML
    private JFXTextField searchField;

    @FXML
    private TableColumn<Book, String> author;


    @FXML
    private TableColumn<Book, String> ISBN;

    @FXML
    private JFXButton addNewBookButton;

    @FXML
    private JFXButton searchBookButton;

    @FXML
    private TableColumn<Book, String> name;

    @FXML
    private TableColumn<Book, String> publisher;

    @FXML
    private TableColumn<Book, String> NowQuantity;

    @FXML
    private TableColumn<Book, String> publicationDate;

    @FXML
    private TableView<Book> table;
    private ObservableSet<Book> bookSet;
    private ObservableList<Book> tableItems;

    @FXML
    void deleteBook(ActionEvent event) {

    }

    @FXML
    void showAll(ActionEvent event) {
        tableItems.setAll(bookSet);
    }

    @FXML
    void addNewBook(ActionEvent event) {
        mainUI.loadContentPane("./Admin/newBookPane.fxml");
        ((newBookController)oneInstanceController.getController("newBookController")).initAuthorBox();

    }

    @FXML
    void searchBook(ActionEvent event) {
        HashSet<Book>ans=Book.getBookFromDBByName(searchField.getText());
        tableItems.setAll(ans);
    }
    public void InitTable()
    {
        InitColumn();
        bookSet=FXCollections.observableSet(Book.getAllBooksFromDB());
        tableItems=FXCollections.observableList(new ArrayList<>(bookSet));

        table.setItems(tableItems);

    }
    public void InitColumn()
    {
        ISBN.setCellValueFactory(new PropertyValueFactory("ISBN"));
        ISBN.setCellFactory(TextFieldTableCell.forTableColumn());

        author.setCellValueFactory(param -> {
            Set<Author> authors=param.getValue().getAuthor();
            String ans="";
            for (Author a:authors) {
                ans=String.join(ans,a.getName(),",");
            }
            return new SimpleStringProperty(ans);
        });
        author.setCellFactory(TextFieldTableCell.forTableColumn());

        publisher.setCellValueFactory(new PropertyValueFactory("publisher"));
        publisher.setCellFactory(TextFieldTableCell.forTableColumn());

        publicationDate.setCellValueFactory(new PropertyValueFactory("publicationDate"));
        publicationDate.setCellFactory(TextFieldTableCell.forTableColumn());


        name.setCellValueFactory(new PropertyValueFactory("name"));
        name.setCellFactory(TextFieldTableCell.forTableColumn());

        NowQuantity.setCellValueFactory(param ->  new SimpleStringProperty(""+param.getValue().getNowQuantity()));
        NowQuantity.setCellFactory(TextFieldTableCell.forTableColumn());

        AllQuantity.setCellValueFactory(param -> new SimpleStringProperty(""+param.getValue().getAllQuantity()));
        AllQuantity.setCellFactory(TextFieldTableCell.forTableColumn());
    }

    public void configBook(ActionEvent actionEvent) {
        Book b=table.getSelectionModel().getSelectedItem();
        if(b!=null)
        {
            mainUI.loadConfigBookPane(b);

        }
    }
}

